<?php
namespace people;

use bank\Bank;
use Exception;
use products\Deposit;
use products\ShortDeposit;
use products\LongDeposit;
use products\Credit;
use products\ConsumerCredit;
use products\HomeCredit;

class Client extends Person {
	
	private $cash;
	private $salary;
	private $deposits = array();
	private $credits = array();
	
	public function __construct($name, $address, $cash, $salary) {
		
		parent::__construct($name, $address);
		$this->setCash($cash);
		$this->setSalary($salary);
	}
	
	public function __destruct() {
	
	}
	
	public function setCash($cash) {
		
		if($cash == "" || $cash == NULL) {
			
			throw new Exception("Cash parameter can't contain a null value or an empty string!");
		}
		
		if(!is_numeric($cash)) {
			
			throw new Exception("Cash parameter must be a numeric value!");
		}
		
		if($cash < 0) {
			
			throw new Exception("Cash parameter must be a positive number!");
		}
		
		$this->cash = $cash;
	}
	
	public function getCash() {
		
		return $this->cash;
	}
	
	public function setSalary($salary) {
		
		if($salary == "" || $salary == NULL) {
				
			throw new Exception("Salary parameter can't contain a null value or an empty string!");
		}
		
		if(!is_numeric($salary)) {
				
			throw new Exception("Salary parameter must be a numeric value!");
		}
		
		if($salary < 0) {
				
			throw new Exception("Salary parameter must be a positive number!");
		}
		
		$this->salary = $salary;
	}
	
	public function getSalary() {
		
		return $this->salary;
	}
	
	public function openDeposit($depositType, $depositAmount, Bank $bank) {
		
		$bank->addDeposit($depositType, $depositAmount, $this);
	}

	public function addDeposit(Deposit $deposit) {
		
		$depositName = $deposit->getAccountName();
		$this->deposits[$depositName] = $deposit;
		$this->cash -= $deposit->getCurrentMoneyAmount();
		
	}
	
	public function getDeposits() {
		
		return $this->deposits;
	}
	
	public function askForCredit($creditType, $creditAmount, $creditPeriod, Bank $bank) {
		
		$bank->addCredit($creditType, $creditAmount, $creditPeriod, $this);
		
	}
	
	public function addCredit($credit) {
		
		$creditName = $credit->getAccountName();
		$this->credits[$creditName] = $credit;
	}
	
	public function getCredits() {
	
		return $this->credits;
	}
	
	public function payCredit($creditName) {
		
		$isThere = false;
		foreach($this->credits as $credit) {
			
			if($credit->getAccountName() == $creditName) {
				
				$isThere = true;
				break;
			}
		}
		
		if($isThere === false) {
			
			throw new Exception("You don't have credit with this name!");
		}
		
		if($this->cash < $this->credits[$creditName]->getMonthlyPayment()) {
			
			throw new Exception("You don't have enough money to pay this credit!");
		}
		
		$this->cash -= $this->credits[$creditName]->getMonthlyPayment();
		$currentDebt = $this->credits[$creditName]->getCurrentDebt();
		$this->credits[$creditName]->setCurrentDebt($currentDebt + $credit->getMonthlyPayment());

	}
	
	public function __toString() {
		
		$str = "";
		$str .= "<p>Client:</p>";
		$str .= "<p>Name:".$this->getName()."</p>";
		$str .= "<p>Cash:".$this->getCash()."</p>";
		$str .= "<p>Salary:".$this->getSalary()."</p>";
		
		if (sizeof($this->deposits) > 0) {
				
			$str .= "<p>Deposits: </p>";
			foreach($this->deposits as $deposit) {
		
				$str .= "<p> Deposit ". $deposit->getAccountName()." - Available money: ". $deposit->getCurrentMoneyAmount()."</p>";
			}
		} else {
				
			$str .= "<p>This client has no deposits!</p>";
		}
		
		if (sizeof($this->credits) > 0) {
		
			$str .=  "<p>Credits: </p>";
			foreach($this->credits as $credit) {
		
				$str .= "<p> Credit ". $credit->getAccountName()." - Money granted: ". $credit->getCurrentMoneyAmount()." - Current Debt: ".$credit->getCurrentDebt()."</p>";
			}
		} else {
				
			$str .= "<p>This client has no credits!</p>";
		}
		
		return $str;
	}
}